// 版权归 瞄你个汪 所有。使用本代码应遵守相关法律法规和Apache 2.0开源许可要求。
package top.geeke.mapper;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import top.geeke.entity.SysMenu;
import top.geeke.eunm.SysMenuTypeEnum;
import top.geeke.mapper.dto.MenuOutput;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 系统菜单表 DAO接口
 * </p>
 * @author 瞄你个汪
 * @since 2024-05-01 12:00:00
 */
@Mapper
public interface SysMenuMapper extends BaseMapper<SysMenu> {
    @Select("SELECT DISTINCT m.* FROM sys_menu m " +
            "INNER JOIN sys_role_menu rm ON rm.menu_id = m.id " +
            "INNER JOIN sys_user_role ur ON ur.role_id = rm.role_id " +
            "WHERE m.`type` != 'Btn' AND ur.user_id = #{userId}")
    List<MenuOutput> getMenuList(@Param("userId")Long userId);

    @Select("SELECT DISTINCT m.permission FROM sys_menu m " +
            "INNER JOIN sys_role_menu rm ON rm.menu_id = m.id " +
            "INNER JOIN sys_user_role ur ON ur.role_id = rm.role_id " +
            "WHERE m.`type` = 'Btn' AND ur.user_id = #{userId}")
    List<String> getPermissions(@Param("userId")Long userId);

    default List<String> getAllPermissions() {
        return selectList(new QueryWrapper<SysMenu>().lambda()
                .eq(SysMenu::getType, SysMenuTypeEnum.Btn)
                .ne(SysMenu::getType, null)
                .select(SysMenu::getPermission))
                .stream().map(SysMenu::getPermission)
                .collect(Collectors.toList());
    }

    default List<MenuOutput> getAllMenuList() {
        return BeanUtil.copyToList(selectList(new QueryWrapper<SysMenu>().lambda()
                .ne(SysMenu::getType, SysMenuTypeEnum.Btn)
        ), MenuOutput.class);
    }
}
